package tom.jiafei;
import java.sql.*;
public class QueryCount{
	String userid;
	int  shoppingCount;
	int  bookCount;
	public void setUserid(String str){
		userid=str;
	}
	public String getUserid(){
		return userid;
	}
	public int getShoppingCount(){
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
    try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=book_store";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rs=sql.executeQuery("SELECT * from shopping where userid='"+userid+"'");
		rs.last();
		shoppingCount=rs.getRow();
    }
	catch(SQLException e1){}
	return shoppingCount;
	}
	public int getBookCount(String bookno){
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){bookCount=100;}
   
    try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=book_store";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
		rs=sql.executeQuery("select count_ from shopping where userid='"+userid+"' and bookno='"+bookno+"'");
		rs.next();
		bookCount=Integer.valueOf(rs.getString(1).trim());
    }
	catch(SQLException e1){bookCount=200;}
	return bookCount;
	}
	
}